import { XmToast } from "@/components/public";
type UserInfo = {
  username: string;
  birthday: string;
};
type ValidationRule<T> = {
  key: keyof T;
  value: T[keyof T];
  validator: (value: T[keyof T]) => boolean;
  errorMessage: string;
};

export function validateData<T>(data: T, rules: ValidationRule<T>[]): boolean {
  for (const rule of rules) {
    const { key, value, validator, errorMessage } = rule;
    if (!validator(value)) {
      console.error(errorMessage);
      XmToast.fail(errorMessage)
      return false;
    }
    data[key] = value; // 更新数据对象中的值
  }
  return true;
}

export function validateUserInfo(userInfo:UserInfo): boolean {
  const rules: ValidationRule<typeof userInfo>[] = [
    {
      key: 'username',
      value: userInfo.username,
      validator: (value) => !!value,
      errorMessage: '姓名不能为空',
    },
    {
      key: 'birthday',
      value: userInfo.birthday,
      validator: (value) => !!value,
      errorMessage: '生日必须填写',
    },
  ];

  return validateData(userInfo, rules);
}
